/*
 * MotorFisica.cpp
 *
 *  Created on: 13/10/2013
 *      Author: Juliansci
 */

#include "MotorFisica.h"

MotorFisica::MotorFisica(b2World* world, float x, float y, float ancho, float alto, string tipo) : EntidadFisica(tipo) {
	//motor
	this->definicionCuerpoFisico->position.Set(x, y);
	this->definicionCuerpoFisico->type =  b2_staticBody;
	this->cuerpoFisico = world->CreateBody(definicionCuerpoFisico);

	b2PolygonShape box;
	box.SetAsBox(ancho/2, alto/2);

	this->fixture = new b2FixtureDef();
	this->fixture->shape = &box;
	this->fixture->density= 1;
	this->fixture->isSensor= true;
	this->cuerpoFisico->CreateFixture(this->fixture);


	b2BodyDef* definicionCuadradito= new b2BodyDef();
	definicionCuadradito->position.Set(x,y);
	definicionCuadradito->type =  b2_staticBody;
	b2Body* cuerpoCuadradito = world->CreateBody(definicionCuadradito);
	b2PolygonShape caja;
	caja.SetAsBox(2.0f, 2.0f);
	b2FixtureDef* fixtureCuadradito= new b2FixtureDef();
	fixtureCuadradito->shape = &caja;
	fixtureCuadradito->isSensor= true;
	fixtureCuadradito->density= 0.5f;
	cuerpoCuadradito->CreateFixture(fixtureCuadradito);

	this->RotaADerecha = true;

	this->guardarInformacionDeEntidad();
	this->alto = alto;
	this->ancho = ancho;

}

void MotorFisica::jointear(b2World* world){
	//jointear motor con engranaje
	cout<<"LE DOY POTENCIA"<<endl;
	EngranajeFisica* engranajeFisica = (EngranajeFisica*)this->aJointearInicial;
	b2Body* amarre = engranajeFisica->getAmarre();
	b2Body* engranaje = engranajeFisica->obtenerCuerpo();
	b2RevoluteJointDef* jointVieja = engranajeFisica->getRevoluteJointDef();
	delete jointVieja;

	b2RevoluteJointDef* jointNueva = new b2RevoluteJointDef();
	jointNueva->Initialize(amarre, engranaje, engranaje->GetPosition());//this->cuerpoFisico->GetPosition());

	jointNueva->collideConnected = true;
	jointNueva->maxMotorTorque = 999999;

	if (this->RotaADerecha) {
		jointNueva->motorSpeed = -5;
	} else {
		jointNueva->motorSpeed = 5;
	}

	jointNueva->enableMotor=true;
	cout<<"jointeo motor"<<endl;
	engranajeFisica->setNuevaJoint(world->CreateJoint(jointNueva));


}

MotorFisica::~MotorFisica() {
	// TODO Auto-generated destructor stub
}

